//
//  ApiVC.m
//  MyStock
//
//  Created by 刘翔 on 2024/9/22.
//

#import "ApiVC.h"
#import <TTGTags/TTGTextTagCollectionView.h>

@interface ApiVC ()
@property (weak, nonatomic) IBOutlet TTGTextTagCollectionView *tagView;
@property (weak, nonatomic) IBOutlet UITextView *textView;

@end

@implementation ApiVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.customNavBar.title = @"API";
    
    NSArray *tags = @[@"logincount", @"updateseleted", @"updatecandidate", @"getseleted", @"getcandidate", @"getstocks",@"updateredyellow",@"updateallstocks",@"startrecordvideo"];
    
    self.tagView.alignment = TTGTagCollectionAlignmentLeft;
    self.tagView.contentInset = UIEdgeInsetsMake(10, 10, 10, 10);
    self.tagView.translatesAutoresizingMaskIntoConstraints = NO;
    self.tagView.layer.cornerRadius = 5;
    self.tagView.layer.masksToBounds = YES;
    
    self.textView.layer.cornerRadius = 5;
    self.textView.layer.masksToBounds = YES;

    NSMutableArray *textTags = [NSMutableArray new];
    for (NSString *string in tags) {
        TTGTextTagStyle *style = [TTGTextTagStyle new];
        style.extraSpace = CGSizeMake(20, 20);
        style.backgroundColor = [UIColor whiteColor];
        
        TTGTextTagStringContent *content = [TTGTextTagStringContent contentWithText:string];
        content.textFont = [UIFont systemFontOfSize:14];
        content.textColor = Color_Theme;
        
        TTGTextTag *textTag = [TTGTextTag tagWithContent:content style:style];
        textTag.onSelectStateChanged = ^(_Bool selected) {
           
            [self loadDataWithApi:textTag.content.getContentAttributedString.string];
        };
        [textTags addObject:textTag];
    }
    [_tagView addTags:textTags];
    [_tagView reload];

}

- (void)loadDataWithApi:(NSString *)api{
    BADataEntity *entity = [BADataEntity new];
    entity.urlString = [NSString stringWithFormat:@"%@/%@",MYSTOCKUrl,api];
    entity.needCache = NO;
    entity.parameters = nil;
    
    [MBProgressHUD showHUDAddedTo:lxWindow animated:YES];
    [BANetManager ba_request_GETWithEntity:entity successBlock:^(id response) {
        NSDictionary *result = response;
        if ([result[@"code"] intValue] == 1) {
            
            NSString *str = [NSString stringWithFormat:@"%@",result];
            self.textView.text = str;
            
        }
        
    } failureBlock:^(NSError *error) {
        self.textView.text = @"";

        
    } progressBlock:^(int64_t bytesProgress, int64_t totalBytesProgress) {
        
    }];
    
}



@end
